(function(){
	
	function sr_prefill_data_first(data){
		var lrnForm1 = jQuery('#sr_lrn1F');
		lrnForm1.find('input.sr_email').val(data.Email);
		lrnForm1.find('input.sr_vemail').val(data.Email);
    }
	
	function sr_prefill_data_second(data){
        var lrnForm2 = jQuery('#sr_lrn2F');
        var inputsMap = {
        	"sr_firstname": "FirstName",
        	"sr_lastname": "LastName",
        	"sr_address1": "BillingStreetAddress1",
        	"sr_address2": "BillingStreetAddress2",
        	"sr_city": "City",
        	"sr_postalcode": "PostalCode",
        	"sr_billingphone": "Phone"
        };
        var selectsMap = {
        	"sr_state": "State",
        	"sr_country": "Country",
        	"sr_birthyear": "YearOfBirthday"
        };
        
        jQuery.each(inputsMap, function(k, v){
        	lrnForm2.find('input.' + k).val(data[v]);
        })

        jQuery.each(selectsMap, function(k, v){
        	lrnForm2.find('select.' + k).val(data[v]);
        })
        
    }
	
	function sr_open_redefine() {
		sr_$.open = function(modal, loc) {

            //set a click location, if exists
            if (typeof(loc) != "undefined" && loc != '') {
                sr_$.conf.click_location = loc;
            }
            else {
                sr_$.conf.click_location = '';
            }
            //create shell if doesnt exist
            if (sr_$.conf.modal.active)return false;
            if (!sr_$.conf.modal.active && !sr_$.$('sr_modal').get()) {
                sr_$.$('sr_global').append(sr_$.modal_shell());//add sr modal shell to document
                sr_$.conf.m_o = sr_$.$('sr_modal');
                //loads the sprite in from css, then turns off
                sr_$.$('sr_bg').bind('click', function() {
                    sr_$.close(true)
                })
            }

            sr_$.conf.last_scroll = sr_$.scroll_T();
            document.documentElement.style.overflow = 'hidden';  // disable scrollbars firefox, chrome
            document.body.scroll = "no"; // disable scrollbars - ie only
            sr_$.conf.m_o.show();
            sr_$.conf.modal.open = true;

            var sbs = sr_$.$('sr_box_screen');//set up the white overlay
            sbs.show();
            sbs.css('opacity', 100);

            sr_$.conf.m_o.css('width', '100px');
            sr_$.conf.m_o.css('height', '100px');
            var doch = sr_$.docH();
            sr_$.conf.m_o.hide();

            var openModal = sr_$.modal(modal);
            if (!openModal)return false;
            if (openModal == "loading") {
                //if this modal is retrieving content, enable loading box.
                sr_$.$('sr_navLinks').html('');
                sr_$.$('sr_box_content').append('<div id="sr_loadingScript"><div class="sr_center"><div class="sr_icon"></div><span>loading...</span></div></div>');

            } else {
                //removing loading box
                sr_$.$('sr_box_content').remove("sr_loadingScript");
            }
            var bg_o = sr_$.$('sr_bg');
            bg_o.css('top', sr_$.conf.last_scroll + "px");
            window.scrollTo(0, sr_$.conf.last_scroll);
            var toolbar = sr_$.$('shoprunner-plugin-toolbar')
            if (toolbar.get()) {
                sr_$.conf.toolbar_zindex = toolbar.css('z-index');
                toolbar.css('z-index', 1);
            }

            bg_o.show();
            if (sr_$.IE && sr_$.IE < 9) {
                bg_o.css('opacity', sr_$.conf.bg_o);
                sr_$.conf.m_o.show();
                sr_$.modal_center();
            } else {
                bg_o.css('opacity', 0);
                bg_o.fade(sr_$.conf.bg_o, 500, 'out');
                setTimeout(function() {
                    sr_$.conf.m_o.fadeIn(400, 'out',function() {
                        sr_$.events.fire('learnOpened');
                    });
                    sr_$.modal_center();
                }, 200);
            }

        };
	}
	
	/* -------------------------------------- */
	/* DO NOT MODIFY ANYTHING BELOW THIS LINE */
	/* -------------------------------------- */
	_shoprunner_com.prefix = window.parent.document.location.protocol + '//';
	if (_shoprunner_com.enabled){
		switch(_shoprunner_com.environmentID){
			case 0:
				var sr_jsOrgURL = 'https://shoprunner.srdev3.cs0.force.com';
				break;
			case 1:
				if(_shoprunner_com.prefix == 'https://') {
					var sr_jsOrgURL = 'https://shoprunner-staging.secure.force.com';
				} else {
				   var sr_jsOrgURL = 'http://shoprunner-staging.force.com';
				}
				break;
			case 2:
				if(_shoprunner_com.prefix == 'https://') {
					var sr_jsOrgURL = 'https://shoprunner.secure.force.com';
				} else {
					var sr_jsOrgURL = 'https://shoprunner-staging.secure.force.com';
				}
				break;
			default:
				var sr_jsOrgURL = _shoprunner_com.SFDCDefaultURL;
				break;
		}
		
		var sr_CSS_URL=sr_jsOrgURL + '/ShopRunnerCss'+_shoprunner_com.retailerID;
		var sr_js_content_el_URL=sr_jsOrgURL + '/JsContentElements'+_shoprunner_com.retailerID;

		setTimeout(function(){
			var s1 = document.createElement("link");
			s1.href = sr_CSS_URL;
			s1.type = "text/css";
			s1.rel="stylesheet";
			document.getElementsByTagName('head')[0].appendChild(s1);
			var so = document.createElement("script");
			so.src = sr_js_content_el_URL;
			so.type = "text/javascript";
			document.getElementsByTagName('head')[0].appendChild(so);
		},100);
	}
	_shoprunner_com.docReady=false;
	function dom_loaded(){
		_shoprunner_com.docReady = true;
		
		if(typeof(sr_$)!=="undefined"){
				sr_$.run();
				sr_open_redefine();
				sr_$.events.addListener('learnOpened', function(){
					var json_url = 'https://gsi-xp-16:444/is-bin/INTERSHOP.enfinity/WFS/GSI-storeaccelerator-Site/en_US/-/USD/ViewShopRunner-GetData';
					jQuery.get(json_url, function(result){
						sr_prefill_data_first(result);
						sr_prefill_data_second(result);
					});
		        });
			}
	}

	if ( document.addEventListener ) {
		document.addEventListener( "DOMContentLoaded", dom_loaded, false );
	}else if ( document.attachEvent ) {
		document.attachEvent( "onreadystatechange", dom_loaded);
	}

	if(window.addEventListener){
		window.addEventListener("load", dom_loaded, false);
	}else if(window.attachEvent){
		var r=window.attachEvent("onload", dom_loaded);
	}

}())